package com.ccy.xxljobdemo.job;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import groovy.util.logging.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class HelloJob {

    @Value("${server.port}")
    private String port;

    @XxlJob("ccyJobHandler")
    public void helloJob() {
        System.out.println("简单任务执行了====>" + port + ":" + LocalDateTime.now());
    }

    @XxlJob("shardinghandler")
    public void shardingJob() {

        //分片参数
        int shardIndex = XxlJobHelper.getShardIndex();
        int shardTotal = XxlJobHelper.getShardTotal();

        //业务逻辑
        List<Integer> list = getList();
        for (Integer integer : list) {
            if (integer % shardTotal == shardIndex) {
                System.out.println("第" + shardIndex + "分片执行了,任务项为:" + integer);
            }
        }
    }

    public List<Integer> getList() {

        List<Integer> list = new ArrayList<>();

        for (int i = 0; i < 1000; i++) {
            list.add(i);
        }

        return list;
    }
}
